﻿// -----------------------------------------------------------------------
// <copyright file="AdvancedSettingsWindow.cs" company="SoftServe">
// window for load balancer settings window
// </copyright>
// -----------------------------------------------------------------------
namespace WebServer
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Input;
    using WebServer.BusinessLogic;

    /// <summary>
    /// Interaction logic for AdvancedSettingsWindow.xaml
    /// </summary>
    public partial class AdvancedSettingsWindow : Window
    {
        public AdvancedSettingsWindow()
        {
            InitializeComponent();

            this.listBoxLoadBalancerAddresses.ItemsSource = Configurator.Instance.LoadBalancerAddresses;
            this.listBoxRedirectAddresses.ItemsSource = Configurator.Instance.RemoteServers;

            this.textBoxLoadBalancerAskingPort.Text = Configurator.Instance.LoadBalancerAskingPort.ToString(CultureInfo.InvariantCulture);
            this.textBoxLoadBalancerAnsweringPort.Text = Configurator.Instance.LoadBalancerAnsweringPort.ToString(CultureInfo.InvariantCulture);
            this.textBoxLoadBalancerTimeout.Text = Configurator.Instance.LoadBalancerTimeOut.ToString(CultureInfo.InvariantCulture);

        }

        private void buttonAddLoadBalancerAddress_Click(object sender, RoutedEventArgs e)
        {
            this.AddLoadBalancerAddress(this.textBoxNewLoadBalancerAddress.Text);
            this.textBoxNewLoadBalancerAddress.Clear();
        }

        private void AddLoadBalancerAddress(string address)
        {
            address = address.Trim();
            if (!address.StartsWith("http://"))
            {
                address = address.Insert(0, "http://");
            }
            if (!Configurator.Instance.LoadBalancerAddresses.Contains(address))
            {
            Configurator.Instance.LoadBalancerAddresses.Add(address);
            }
        }

        private void AddRedirectAddress(string address)
        {
            address = address.Trim();
            if (!address.StartsWith("http://"))
            {
                address = address.Insert(0, "http://");
            }
            if (!Configurator.Instance.RemoteServers.Contains(address))
            {
                Configurator.Instance.RemoteServers.Add(address);
            }
        }

        private void buttonDeleteLoadBalancerAddress_Click(object sender, RoutedEventArgs e)
        {
            if (this.listBoxLoadBalancerAddresses.SelectedIndex >= 0)
            {
                this.DeleteLoadBalancerAddress();
            }
        }

        private void buttonDeleteRedirectAddress_Click(object sender, RoutedEventArgs e)
        {
            if (this.listBoxRedirectAddresses.SelectedIndex >= 0)
            {
                this.DeleteRedirectAddress();
            }
        }

        private void textBoxNewLoadBalancerAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.AddLoadBalancerAddress(this.textBoxNewLoadBalancerAddress.Text);
                this.textBoxNewLoadBalancerAddress.Clear();
            }
        }

        private void listBoxLoadBalancerAddresses_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.listBoxLoadBalancerAddresses.SelectedItem != null && e.Key == Key.Delete)
            {
                this.DeleteLoadBalancerAddress();
            }
        }

        private void DeleteLoadBalancerAddress()
        {
                int tempIndex = this.listBoxLoadBalancerAddresses.SelectedIndex;
                Configurator.Instance.LoadBalancerAddresses.Remove(this.listBoxLoadBalancerAddresses.SelectedItem.ToString());
                this.listBoxLoadBalancerAddresses.SelectedIndex = tempIndex == 0 ? tempIndex : tempIndex - 1;
        }

        private void DeleteRedirectAddress()
        {
            int tempIndex = this.listBoxRedirectAddresses.SelectedIndex;
            Configurator.Instance.RemoteServers.Remove(this.listBoxRedirectAddresses.SelectedItem.ToString());
            this.listBoxRedirectAddresses.SelectedIndex = tempIndex == 0 ? tempIndex : tempIndex - 1;
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            Configurator.Instance.LoadBalancerAskingPort = Convert.ToInt32(this.textBoxLoadBalancerAskingPort.Text);
            Configurator.Instance.LoadBalancerAnsweringPort = Convert.ToInt32(this.textBoxLoadBalancerAnsweringPort.Text);
            Configurator.Instance.LoadBalancerTimeOut = Convert.ToInt32(this.textBoxLoadBalancerTimeout.Text);

            MyHttpServer.Restart(); // Can be redundand
            this.Close();
        }

        private void buttonAddRedirect_Click(object sender, RoutedEventArgs e)
        {
            this.AddRedirectAddress(this.textBoxNewRedirectAddress.Text);
            this.textBoxNewRedirectAddress.Clear();
        }

        private void buttonDeleteRedirect_Click(object sender, RoutedEventArgs e)
        {
            if (this.listBoxRedirectAddresses.SelectedIndex >= 0)
            {
                this.DeleteRedirectAddress();
            }
        }

        private void listBoxRedirectAddresses_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.listBoxRedirectAddresses.SelectedItem != null && e.Key == Key.Delete)
            {
                this.DeleteRedirectAddress();
            }
        }

        private void textBoxNewRedirectAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.AddRedirectAddress(this.textBoxNewRedirectAddress.Text);
                this.textBoxNewRedirectAddress.Clear();
            }
        }
    }
}
